package cn.edu.bjfu.it.oyss.covid_19helper;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.edu.bjfu.it.oyss.covid_19helper.bean.ProvinceInfo;
import cn.edu.bjfu.it.oyss.covid_19helper.utils.ApiService;
import cn.edu.bjfu.it.oyss.covid_19helper.utils.CovidDataBaseHelper;
import cn.edu.bjfu.it.oyss.covid_19helper.utils.ProvincesHelper;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.jetbrains.annotations.NotNull;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;
import java.util.ArrayList;


public class StateActivity extends AppCompatActivity {
    private String id;
    private String today;
    private String total;
    private TextView today_t;
    private TextView total_t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state);


        id = getIntent().getStringExtra(ProvincesNameActivity.PROVINCES_ID_KEY);    //设置省名和照片，保存id省名
        String name = ProvincesHelper.getInstance(this).getProvinceNameById(id,this);               //
        TextView provinceName = findViewById(R.id.ProvinceName);                             //
        provinceName.setText(name);
        int photoId = ProvincesHelper.getInstance(this).getProvincePhotoById(id,this);
        ImageView photo = findViewById(R.id.photo);
        if(photoId!=0) photo.setImageResource(photoId);
        int riskLevel = ProvincesHelper.getInstance(this).getRISKById(id, this);
        RadioGroup riskLevelRadio = findViewById(R.id.riskLevelRadio);
        RadioButton highRisk,midRisk,lowRisk;
        highRisk = findViewById(R.id.highRisk);
        midRisk = findViewById(R.id.midRisk);
        lowRisk = findViewById(R.id.lowRisk);
        switch (riskLevel){
            case 0:
                riskLevelRadio.check(lowRisk.getId());
                break;
            case 1:
                riskLevelRadio.check(highRisk.getId());
                break;
            case 2:
                riskLevelRadio.check(midRisk.getId());
                break;
        }

        riskLevelRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                                                      @Override
                                                      public void onCheckedChanged(RadioGroup group, int checkedId) {
                                                          if(checkedId==highRisk.getId()) ProvincesHelper.getInstance(getParent()).setRISKById(id,getParent(),1);
                                                          if(checkedId==midRisk.getId()) ProvincesHelper.getInstance(getParent()).setRISKById(id,getParent(),2);
                                                          if(checkedId==lowRisk.getId()) ProvincesHelper.getInstance(getParent()).setRISKById(id,getParent(),0);
                                                      }
                                                  }
        );


        Retrofit retrofit = getRetrofit();                                          //构建Retrofit，修改User-Agent
        ApiService apiService = retrofit.create(ApiService.class);                             //构建apiService
        Call<ProvinceInfo> ProvinceInfoCall = apiService.getProvinceInfo();         //构建Call
        ProvinceInfoCall.enqueue(new Callback<ProvinceInfo>() {                     //请求
                                     @Override
                                     public void onResponse(@NotNull Call<ProvinceInfo> call, @NotNull Response<ProvinceInfo> response) {
                                         ProvinceInfo provinceInfo = response.body();//拿到接口数据
                                         assert provinceInfo != null;
                                         ArrayList<ProvinceInfo.Data.Country.City> children = //中国数据
                                                 provinceInfo.getData().getAreaTree().get(2).getChildren();
                                         for (ProvinceInfo.Data.Country.City child : children) {
                                             if (id.equals(child.getId())) {
                                                 today = child.getToday().getConfirm();
                                                 total = child.getTotal().getConfirm();
                                                 today_t = findViewById(R.id.today);
                                                 total_t = findViewById(R.id.total);
                                                 today_t.setText(today);
                                                 total_t.setText(total);
                                                 break;
                                             }
                                         }
                                     }

                                     @Override
                                     public void onFailure(@NotNull Call<ProvinceInfo> call, @NotNull Throwable t) {

                                     }
                                 }
        );

    }


    @NotNull
    private Retrofit getRetrofit() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder() ;//https://blog.csdn.net/linyaodai/article/details/78585121
        builder.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder()
                        .removeHeader("User-Agent")
                        .addHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4)")
                        .build() ;
                return chain.proceed(newRequest);
            }
        }) ;
        OkHttpClient client = builder.build() ;

        return new Retrofit.Builder()
                .baseUrl("https://c.m.163.com")
                .addConverterFactory(GsonConverterFactory.create())//
                .client(client)
                .build();
    }
}